#include "EWADesktopTimeline.hpp"

#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"

#include "EWAUIWidget/EWAUIWidgetHeader.hpp"

namespace Eyer
{
    EWADesktopTimeline::EWADesktopTimeline()
        : timelineCtx(new EWAUITimelineContext())
        , timelineWidget(timelineCtx, LP::MATCH_PARENT, LP::MATCH_PARENT)
    {

    }

    EWADesktopTimeline::~EWADesktopTimeline()
    {

    }

    int EWADesktopTimeline::OnGUI()
    {
        ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
        // ImGui::Begin(u8"Time Line 时间轴");
        ImGui::Begin(u8"UI");
        ImGui::PopStyleVar();

        // Demo UI Start
        if(ImGui::Button("Insert Track")){
            trackIndex++;
            EyerString trackId = EyerString("Track: ") + EyerString::Number(trackIndex);
            timelineWidget.AddTrack(trackId);
        }
        ImGui::SameLine();
        if(ImGui::Button("Insert Fragment")){
            EyerSmartPtr<EWAPrjFragmentFragmentResourse> fragment(new EWAPrjFragmentFragmentResourse());
            timelineWidget.InsertFragment(fragment, 0);
        }
        // Demo UI End


        ImVec2 XY = ImGui::GetCursorScreenPos();
        ImVec2 WH = ImGui::GetContentRegionAvail();

        ImVec2 mousePos = ImGui::GetMousePos();
        ImVec2 viewMousePos(mousePos.x - XY.x, mousePos.y - XY.y);

        ImDrawList* draw_list = ImGui::GetWindowDrawList();

        /**************************************************/

#ifdef EYER_EWAUI_DEBUG
        EyerLog("=================================================\n");
#endif

        timelineWidget.OnMeasure(WH.x, WH.y);
        if(viewMousePos.x >= 0 && viewMousePos.y >= 0){
            bool left = ImGui::IsMouseClicked(ImGuiMouseButton_Left);
            if(left){
                Eyer::Vec2I pos(viewMousePos.x, viewMousePos.y);
                // EyerLog("Press pos: %f, %f\n", viewPos.x, viewPos.y);
                timelineWidget.MousePressEvent(pos);
            }
            left = ImGui::IsMouseReleased(ImGuiMouseButton_Left);
            if(left){
                Eyer::Vec2I pos(viewMousePos.x, viewMousePos.y);
                // EyerLog("Released pos: %f, %f\n", viewPos.x, viewPos.y);
                timelineWidget.MouseReleaseEvent(pos);
            }
            left = ImGui::IsMouseDragging(ImGuiMouseButton_Left);
            if(left){
                Eyer::Vec2I pos(viewMousePos.x, viewMousePos.y);
                // EyerLog("Moving pos: %f, %f\n", viewPos.x, viewPos.y);
                timelineWidget.MouseMoveEvent(pos);
            }
        }

        timelineWidget.Draw();

#ifdef EYER_EWAUI_DEBUG
        timelineWidget.PrintInfo(0);
#endif

        int graphSize = timelineWidget.GetUIGraphSize();
        // EyerLog("Draw Graph Size: %d\n", graphSize);
        for(int i=0;i<graphSize;i++){
            EyerSmartPtr<EWAUIGraph> g = timelineWidget.GetUIGraph(i);

            if(g->GetType() == EWAUIGraphType::RECT){
                EyerSmartPtr<EWAUIGraphRect> gRect = g;

                EyerColor bg = gRect->background;

                auto lineColor = IM_COL32(255, 0, 0, 255);
                auto graphColor = IM_COL32(bg.r() * 255, bg.g() * 255, bg.b() * 255, bg.a() * 255);

                Vec2I offsetXY(XY.x, XY.y);
                Vec2I xy = gRect->xy + offsetXY;
                Vec2I wh = gRect->wh;

                ImVec2 pA(xy.x(), xy.y());
                ImVec2 pB(xy.x() + wh.x(), xy.y() + wh.y());

                draw_list->AddRectFilled(pA, pB, graphColor, gRect->rounding);

                if(gRect->border > 0){
                    draw_list->AddRect(pA, pB, lineColor, gRect->rounding, 0, gRect->border);
                }

#ifdef EYER_EWAUI_DEBUG
                draw_list->AddRect(pA, pB, lineColor, 0, 0, 0);
#endif
            }

            else if(g->GetType() == EWAUIGraphType::LINE){
                EyerSmartPtr<EWAUIGraphLine> gRect = g;

                EyerColor lcolor = gRect->linecolor;

                auto lineColor = IM_COL32(lcolor.r() * 255, lcolor.g() * 255, lcolor.b() * 255, lcolor.a() * 255);
                auto graphColor = IM_COL32(lcolor.r() * 255, lcolor.g() * 255, lcolor.b() * 255, lcolor.a() * 255);

                Vec2I offsetXY(XY.x, XY.y);
                Vec2I xyStart = gRect->xyStart + offsetXY;
                Vec2I xyEnd = gRect->xyEnd + offsetXY;

                ImVec2 pA(xyStart.x(), xyStart.y());
                ImVec2 pB(xyEnd.x(), xyEnd.y());

                draw_list->AddLine(pA, pB, lineColor);
            }

            else if(g->GetType() == EWAUIGraphType::TEXT){
                EyerSmartPtr<EWAUIGraphText> gRect = g;
                EyerColor lcolor = gRect->textcolor;
                Vec2I offsetXY(XY.x, XY.y);
                Vec2I xy = gRect->xy + offsetXY;
                ImVec2 pos(xy.x(), xy.y());
                // auto lineColor = IM_COL32(255, 0, 0, 255);
                auto lineColor = IM_COL32(lcolor.r() * 255, lcolor.g() * 255, lcolor.b() * 255, lcolor.a() * 255);
                draw_list->AddText(pos, lineColor, gRect->str.c_str());
            }
        }
        /**************************************************/

        ImGui::End();

        return 0;
    }

    int EWADesktopTimeline::OnScroll(double x, double y)
    {
        timelineWidget.ScrollEvent(x, y);
        return 0;
    }
}